<?php 
	require_once("connector.php");
	require_once("baseobj.php");
	
	class Task extends BaseObj {
		public $TaskId = 0;
		public $ProjectId = 0;
		public $AssignedUserId = 0;
		public $Name = "";
		public $Description = "";
		public $TaskPriorityId = 0;
		public static $Select = "SELECT TASKID, PROJECTID, ASSIGNEDUSERID, NAME, DESCRIPTION, TASKPRIORITYID, to_char(CREATED,'MM/DD/YYYY HH24:MI') CREATED, to_char(MODIFIED,'MM/DD/YYYY HH24:MI') MODIFIED FROM Task";
		
		public function delete() {
			$sql = "DELETE FROM TASK WHERE TaskId = :taskid";
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':taskid', $this->TaskId);
			$cnt = oci_execute($stmt);
			oci_free_statement($stmt);
			oci_close($conn);
		}
		
		public function load($row){		
			$this->TaskId = $row["TASKID"];
			$this->ProjectId = $row["PROJECTID"];
			$this->AssignedUserId = $row["ASSIGNEDUSERID"];
			$this->Name = $row["NAME"];
			$this->Description = $row["DESCRIPTION"];
			$this->TaskPriorityId = $row["TASKPRIORITYID"];				
			$this->Created = $row["CREATED"];
			$this->Modified = $row["MODIFIED"];
		}
	
		public static function getByUserId($userId){
			$arr = array();
			$i = 0;
			$sql = Task::$Select." 
					WHERE (Task.AssignedUserId = :userId)
						OR (Task.AssignedUserId IS NULL 
							AND EXISTS(
								SELECT *
								FROM Users u
									JOIN Role r ON u.RoleId = r.RoleId
										AND r.Name IN('Admin','Manager')
								WHERE u.UserId = $userId 
							)) ";
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':userId', $userId);

			$cnt = oci_execute($stmt);
			$u = null;
			if($cnt > 0) {				
				while($row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS)) {
					$u = new Task();
					$u->load($row);
					$arr[$i] = $u;
					$i++;
				}
			}			
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $arr;
		}
		
		public static function getFromURL(){
			$taskid = null;
			$task = null;
			if(isset($_GET["taskid"])){ 
				$taskid = $_GET["taskid"];
				$task = Task::getByTaskId($taskid);
			}
			return $task;
		}
		
		public static function getByTaskId($taskId){
			$sql = Task::$Select." WHERE TaskId = :taskId";
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':taskId', $taskId);

			$cnt = oci_execute($stmt);
			$u = null;
			if($cnt > 0) {
				$row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);				
				$u = new Task();
				$u->load($row);
			}			
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $u;
		}
		
		public static function nextId() {
			$sql = "SELECT seq.nextval FROM Dual"; 
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_execute($stmt);
			$row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);
			$v = $row["NEXTVAL"];
			oci_free_statement($stmt);
			oci_close($conn);
			return $v;
		}
		
		public function save() {
			$sql = null;
			
			$this->Modified = new DateTime();
			
			if($this->AssignedUserId == 0)
				$this->AssignedUserId = null;
			
			if($this->TaskId == null || $this->TaskId == 0) {
				$this->TaskId = Task::nextId();
				$sql = "INSERT INTO Task (TaskId, ProjectId, AssignedUserId, Name, Description, TaskPriorityId, Created, Modified) 
				        VALUES (:taskId, :projectId, :assignedUserId, :name, :description, :priority, :modd, :modd)";	
			}
			else {  				
				$sql = "UPDATE Task 
						SET ProjectId = :projectId, 
							AssignedUserId = :assignedUserId, 
							Name = :name, 
							Description = :description, 
							TaskPriorityId = :priority,
							Modified = :modd
						WHERE TaskId = :taskId";	
			}
			
			$f = $this->nowStr();
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':taskId', $this->TaskId);
			oci_bind_by_name($stmt, ':projectId', $this->ProjectId);
			oci_bind_by_name($stmt, ':assignedUserId', $this->AssignedUserId);
			oci_bind_by_name($stmt, ':name', $this->Name);
			oci_bind_by_name($stmt, ':description', $this->Description);
			oci_bind_by_name($stmt, ':priority', $this->TaskPriorityId);
			oci_bind_by_name($stmt, ':modd', $f);
			$cnt = oci_execute($stmt);
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $cnt > 0;
		}
	}
?>